import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author: gao
 * @create-date: 2025/3/2 19:13
 */

public class Solution {
    public List<Integer> findAnagrams(String s,String p){
        List<Integer> ret = new ArrayList<Integer>();
        char[] ss = s.toCharArray();
        char[] pp = p.toCharArray();

        int[] hash1 = new int[26];//统计p中每一个字符出现的个数
        for(char ch : pp) hash1[ch - 'a']++;

        int[] hash2 = new int[26];//统计窗口中字符出现的个数
        for (int left = 0,right = 0,count = 0;right < s.length(); right++){
            char in = ss[right];
            if(++hash2[in - 'a'] <= hash1[in - 'a']) count++;//进窗口与维护count
            if(right - left + 1 > p.length()){//判断
                char out = ss[left];
                if(hash2[out - 'a']-- <= hash1[out - 'a']) count--;//出窗口与维护count
            }
            if(count == p.length()) ret.add(left);
        }
        return ret;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
            List<Integer> result = new ArrayList<>();
            Solution solution = new Solution();
            System.out.println(solution.findAnagrams("cbaebabace", "abc"));
    }
}
